<?php
/**
 * 通知公告
 * User: xsh
 * Date: 2018/4/14  13:39
 */

namespace app\mobile\Controller;
use think\Controller;

class Notice extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    public function zindex()
    {
        $listnum = $this->per_page;//每页条数
        //搜索
        $pageParam = ['query'=>[]];
        $notice_result = [];
        $page = '';


        $province = input('province');//var_dump($province);
        $city_list = '';
        if($province != ''){
            $area[0] = db('area')->where('id', input('province'))->value('name');
            $city_list = db('area')->where('parent_id', $province)->select();
        }
        $pageParam['query']['province'] = $province;

        $city = input('city');//var_dump($city);
        $country_list = '';
        if($city != ''){
            $area[1] = db('area')->where('id', input('city'))->value('name');
            $country_list = db('area')->where('parent_id', $city)->select();
        }
        $pageParam['query']['city'] = $city;

        $country = input('country');
        if($country != ''){
            $area[2] = db('area')->where('id', input('country'))->value('name');
        }
        $pageParam['query']['country'] = $country;

        if(isset($area)){
            $areas = implode(',',$area);
            $where['area'] = array('like','%'.$areas.'%');
        }

        $title = input('title');
        if($title != '')
        {
            $where['title'] =array('like','%'.$title.'%');
        }
        $pageParam['query']['title'] = $title;

        $order = 'desc';
        if(input('order') == '0'){
            $order = '';
        }
        $pageParam['query']['order'] = input('order');

        $notice = db('notice');
        $where['is_display'] = 1;

        $time = input('time',1);
        if($time == 1){
            $where['over_time'] = [['>',time()],['=',0],'or'];
        }elseif($time == 0){
            $where['over_time'] = [['<',time()],['>',0],'and'];
        }
        $pageParam['query']['time'] = $time;

        $department = input('department');
        if($department != '')
        {
            $where['department'] =array('like','%'.$department.'%');
        }
        $pageParam['query']['department'] = $department;
        $this->assign('department', $department);
        //        部门
        $department_list = $notice->distinct(true)->column('department');
        $this->assign('department_list', $department_list);

        $notice_result = $notice->field('id,title,department,take_time,over_time')
            ->where($where)
            ->order('take_time',$order)
            ->paginate($listnum, false, $pageParam);

        $count = $notice_result->lastPage();
        if($count > 1){
            $this->assign('count',$count);
        }
        if(request()->isAjax()){
            if($notice_result){
                return $this->success($notice_result);
            }
            exit;
        }

        $this->assign('time', $time);
        $this->assign('province_list', getProvince());
        $this->assign('city_list', $city_list);
        $this->assign('country_list', $country_list);
        $this->assign('province', $province);
        $this->assign('city',$city);
        $this->assign('country', $country);
        $this->assign('title', $title);
        $this->assign('order', input('order'));
        $this->assign('notice_result',$notice_result);
        return $this->fetch();
    }

    public function detail_continue(){
        if(!$this->info_user['id'])$this->error('请先登录');

        $uid = $this->info_user['id'];

        $id = input('id');

        $result = member_right_num($uid,'circular','consult',false);

        if(!$result['bool']){
            $this->error('你的查询次数已用完或者没权利查询');
        }

        $this->success('查询成功', url('Notice/detail',array('id'=>$id)));
    }


    public function detail(){
        $id = input('id');
        $uid = $this->info_user['id'];
        if(!$this->info_user['id'])$this->error('请先登录');

        $result = member_right_num($uid,'circular','consult',false);
        if(!$result['bool']){
            $this->error('你的查询次数已用完或者没权利查询');
        }

        $data = db('notice')->alias('n')
            ->field('n.*')
            ->where('n.id',$id)
            ->find();

        $file_result = db('file')->where(array('id'=>$data['file_id']))->field('file_name')->find();
        if(empty($file_result)){
            $file_result = db('file')->where(array('id'=>$data['down_id']))->field('file_name')->find();
        }

        $data['file_name'] = $file_result['file_name'];

        //权利减少
        member_right_num($uid,'circular','consult');
        $keyword = db('keywords')->where('notice_id', $id)->field('keywords')->find();
        $this->assign('keyword', $keyword);
        $this->assign('data', $data);
        $this->assign('id',$id);
        return $this->fetch('notice/detail');
    }

    public function pdf_preview_cotinue(){
        if(request()->isAjax()) {
            if(!isset($this->info_user['id']))$this->error('请先登录在预览');
        }

        //权限限制
        $result = member_right_num($this->info_user['id'],'circular','enclosure');

        if(isset($result['bool'])){
            if(!$result['bool']){
                $this->error('你的预览次数已用完或者你没有权限');
            }
        }
    }


    //pdf预览
    public function pdf_preview(){
        $id = input('id');

        $data = db('notice')->alias('n')
            ->join('file f','f.id = n.file_id','LEFT')
            ->field('f.file_path')
            ->where('n.id',$id)
            ->find();

        $this->assign('data', $data);
        return $this->fetch();
    }

    //过滤条件
    public function down_continue(){
        $id = input('id');

        if(request()->isAjax()) {
            if(!isset($this->info_user['id']))$this->error('请先登录在下载');
        }

        //权限限制
        $result = member_right_num($this->info_user['id'],'circular','down',false);

        if(isset($result['bool'])){
            if(!$result['bool']){
                $this->error('你的下载次数已用完或者你没有权限');
            }
        }

        $data = db('notice')->alias('n')
            ->join('file f','f.id = n.down_id','LEFT')
            ->field('f.file_path,f.file_size,f.file_name,f.id as fid')
            ->where('n.id',$id)
            ->find();

        //下载记录
        $down_data['type'] = 2; 
        $down_data['fid'] = $data['fid'];
        $down_data['uid'] = $this->info_user['id'];
        $down_data['pid'] = $id;
        $down_data['time'] = time();

        $result = db('down_record')->insert($down_data);

        if(!$result)$this->error('下载失败');
        $this->success();
    }

    //down下载
    public function down_pdf(){
        $id = input('id');

        //权限限制
        $result = member_right_num($this->info_user['id'],'circular','down',false);

        if(isset($result['bool'])){
            if(!$result['bool']){
                $this->error('你的下载次数已用完或者你没有权限');
            }
        }

        $data = db('notice')->alias('n')
            ->join('file f','f.id = n.down_id','LEFT')
            ->field('f.file_path,f.file_size,f.file_name,f.id as fid')
            ->where('n.id',$id)
            ->find();

        $file = IMG_PATH.$data['file_path'];
        $file_name_suffix = explode('.', $data['file_name']);
        $name = $file_name_suffix[0];

        $fileName = $name ? $name : pathinfo($file,PATHINFO_FILENAME);
        $filePath = $file;

        $fp = fopen($file,'rb');

        if(!$filePath || !$fp){
            header('HTTP/1.1 404 Not Found');
            echo "Error: 404 Not Found.(server file path error)";
            exit;
        }

        $fileName = $fileName .'.'. pathinfo($filePath,PATHINFO_EXTENSION);
        $encoded_filename = urlencode($fileName);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);

        header('HTTP/1.1 200 OK');
        header( "Pragma: public" );
        header( "Expires: 0" );
        header("Content-type: application/octet-stream");
        header("Content-Length: ".$data['file_size']);
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".$data['file_size']);

        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $fileName . '"');
        }

        ob_end_clean(); //<--有些情况可能需要调用此函数
        // 输出文件内容
        fpassthru($fp);

        //权限次数扣除
        member_right_num($this->info_user['id'],'circular','down');
        exit;
    }
}